<?php 
namespace plugins\finance\model;

use think\Model;
use think\Db;
use plugins\pay\lib\Pay;

class FinanceExtractModel extends Model
{	
	//生成提现申请
	public function frozen($finance_id,$extract_aim,$openid,$money,$name)
	{
		$this->extract_num = uniqid('extract');
		$this->finance_id = $finance_id;
		$this->extract_aim = $extract_aim;
		//提现绑定账户
		$this->extract_aim_account = $openid;

		//提现名
		$this->extract_aim_name = $name;

		$this->time = time();
		//根据配置进行判断
		$this->status = 1;

		$this->money = $money;

		$this->log = json_encode([['time'=>time(),'message'=>'提交提现申请']]);

		$return = $this->save();
		return $return ;
	}

	//提现操作
	public function extract()
	{

		$type = $this->extract_aim;

		//支付宝
		if($type == 'alipay'){
			//创建支付类
			$wechatpay= Pay::createInstance(  $type  );	
			$bizcontent = [];
			//订单号
			$bizcontent['out_biz_no'] = $this->extract_num;
			//金额
			$bizcontent['trans_amount'] = $this->money;
			//描述特定的业务场景，可传的参数如下：
			// DIRECT_TRANSFER：单笔无密转账到支付宝/银行卡, B2C现金红包;
			// PERSONAL_COLLECTION：C2C现金红包-领红包
			$bizcontent['biz_scene'] = 'DIRECT_TRANSFER';
			//业务产品码
			//单笔无密转账到支付宝账户固定为:
			// TRANS_ACCOUNT_NO_PWD；
			// 单笔无密转账到银行卡固定为:
			// TRANS_BANKCARD_NO_PWD;
			// 收发现金红包固定为:
			// STD_RED_PACKET；
			$bizcontent['product_code'] = 'TRANS_ACCOUNT_NO_PWD';

			//参与方的唯一标识
			$bizcontent['payee_info']['identity'] = $this->extract_aim_account;

			// 参与方的标识类型，目前支持如下类型：
			// 1、ALIPAY_USER_ID 支付宝的会员ID
			// 2、ALIPAY_LOGON_ID：支付宝登录号，支持邮箱和手机号格式
			$bizcontent['payee_info']['identity_type'] = 'ALIPAY_LOGON_ID';

			//真实姓名
			$bizcontent['payee_info']['name'] = $this->extract_aim_name;

			$bizcontent = json_encode($bizcontent);	

			$res = $wechatpay->earning( $bizcontent );

			return $res;
		}

		//微信
		if($type == 'wechatpay'){
			$wechatpay= Pay::createInstance(  $type  );	//创建pay的class

			$bizcontent = [];
			//订单号
			$bizcontent['partner_trade_no'] = $this->extract_num;
			//提现号
			$bizcontent['nonce_str'] = uniqid();
			//提现金额,分
			$bizcontent['amount'] = $this->money;
			//是否检查真实姓名
			$bizcontent['check_name'] = 'NO_CHECK';
			//openid
			$bizcontent['openid'] = $this->extract_aim_account;

			$bizcontent['desc'] = '提现:'.$this->money;

			$bizcontent = json_encode($bizcontent);	

			$res = $wechatpay->earning( $bizcontent );

			return $res;		
		}

		return ['status'=>false,'message'=>'错误的记录'];
	

	}

}